package com.zlsx.androiddemo.info;

import android.content.Context;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;

/**
 * 获取各种APP信息工具类
 */
public class AppUtil {
    /**
     * 将ip的整数形式转换成ip形式
     *
     * @param ip
     * @return
     */
    public static String formatIp(int ip) {
        StringBuilder sb = new StringBuilder();
        sb.append(ip & 0xFF).append(".");
        sb.append((ip >> 8) & 0xFF).append(".");
        sb.append((ip >> 16) & 0xFF).append(".");
        sb.append((ip >> 24) & 0xFF);
        return sb.toString();
    }

    /**
     * 获取当前ip地址
     *
     * @param context
     * @return
     */
    public static String getAppIp(Context context) {
        try {
            WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
            WifiInfo wifiInfo = wifiManager.getConnectionInfo();
            int i = wifiInfo.getIpAddress();
            return formatIp(i);
        } catch (Exception ex) {
            return " 获取IP出错" + ex.getMessage();
        }
    }
}
